package dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import entity.Data;
import entity.User;
import util.db.DBhelp;
import util.db.RowMapper;
import util.web.Page;

public class DataDao {

	private DBhelp<Data> dBhelp = new DBhelp<Data>();
	
	
	private class DataRowMapper implements RowMapper<Data> {

		public Data mapRow(ResultSet resultSet) throws SQLException {
			Data data = new Data();
			data.setId(resultSet.getString("id"));
			data.setTitle(resultSet.getString("title"));
			data.setContent(resultSet.getString("content"));
			data.setCreatetime(resultSet.getString("createtime"));
			data.setUserid(resultSet.getString("userid"));
			data.setProjectid(resultSet.getString("projectid"));
			return data;
		}
		
	}


	public void save(Data data) {
		String sql = "insert into t_data (id,title,content,createtime,userid,projectid)values(?,?,?,?,?,?)";
		dBhelp.update(sql, data.getId(),data.getTitle(),data.getContent(),data.getCreatetime(),data.getUserid(),data.getProjectid());
	}


	public Page<Data> findReplayByReplayWhoId(int nowPageNum, String id) {
		
		Page<Data> page = new Page<Data>(nowPageNum, countByProjectid(id));
		String sql = "select * from t_data where projectid=? order by createtime desc limit ?,?";
		List<Data> list = dBhelp.executeQueryForList(sql, new RowMapper<Data>() {

			public Data mapRow(ResultSet resultSet) throws SQLException {
				Data data = new Data();
				data.setId(resultSet.getString("id"));
				data.setTitle(resultSet.getString("title"));
				data.setContent(resultSet.getString("content"));
				data.setCreatetime(resultSet.getString("createtime"));
				data.setUserid(resultSet.getString("userid"));
				data.setProjectid(resultSet.getString("projectid"));
				User user = new UserDao().findUserById(resultSet.getString("userid"));
				data.setUser(user);
				return data;
			}
		}, id,page.getStartPage(),page.getPagesize());
		
		page.setList(list);
		
		return page;
	}


	private int countByProjectid(String projectid) {
		String sql = "select count(*) from t_data where projectid=?";
		return dBhelp.executeQuery(sql, projectid).intValue();
	}
	
	

	public Data findDataById(String id) {
		String sql = "select * from t_data where id=?";
		return dBhelp.executeQueryToObject(sql, new RowMapper<Data>() {

			public Data mapRow(ResultSet resultSet) throws SQLException {
				Data data = new Data();
				data.setId(resultSet.getString("id"));
				data.setTitle(resultSet.getString("title"));
				data.setContent(resultSet.getString("content"));
				data.setCreatetime(resultSet.getString("createtime"));
				data.setUserid(resultSet.getString("userid"));
				data.setProjectid(resultSet.getString("projectid"));
				User user = new UserDao().findUserById(resultSet.getString("userid"));
				data.setUser(user);
				return data;
			}
		}, id);
	}


	public Data findDataByIdonly(String id) {
		String sql = "select * from t_data where id=?";
		return dBhelp.executeQueryToObject(sql, new DataRowMapper(), id);
	}


	public void edit(Data data) {
		String sql = "update t_data set title=?,content=?,createtime=? where id=? ";
		dBhelp.update(sql, data.getTitle(),data.getContent(),data.getCreatetime(),data.getId());
	}


	public void removeById(String dataid) {
		String sql = "delete from t_data where id=?";
		dBhelp.update(sql, dataid);
	}
	
	
	
	
	
	
	
}
